let dateUtil = {
  formatDate (value) {
    let date = new Date(value)
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? '0' + MM : MM
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let m = date.getMinutes()
    m = m < 10 ? '0' + m : m
    let s = date.getSeconds()
    s = s < 10 ? '0' + s : s
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  },
  // 日期格式化
  dateFtt (date, fmt) {
    if (date) {
      fmt = fmt.replace(/yyyy/, date.substr(0, 4))
      fmt = fmt.replace(/MM/, date.substr(4, 2))
      fmt = fmt.replace(/dd/, date.substr(6, 2))
      fmt = fmt.replace(/HH/, date.substr(8, 2))
      fmt = fmt.replace(/mm/, date.substr(10, 2))
      fmt = fmt.replace(/ss/, date.substr(12, 2))
    } else {
      fmt = ''
    }
    return fmt
  },

  p (s) {
    return s < 10 ? '0' + s : s
  },

  // 日期转string
  dateStr (date) {
    // 获取当前年
    let year = date.getFullYear()
    // 获取当前月
    let month = date.getMonth() + 1
    // 获取当前日
    let day = date.getDate()
    let h = date.getHours()
    // 获取当前小时数(0-23)
    let m = date.getMinutes()
    // 获取当前分钟数(0-59)
    let s = date.getSeconds()
    return year + '' + this.p(month) + '' + this.p(day) + '' + this.p(h) + '' + this.p(m) + '' + this.p(s)
  },

  // 日期不补零
  dateUnZero (date) {
    if (date) {
      date = date.replace(/[^0-9]/ig, '')
      let num = date.length
      date = this.dateFtt(date, 'yyyy-MM-dd HH:mm:ss')
      let newDate = new Date(date)
      if (isNaN(newDate.getTime())) {
        console.error('Cannot read property \'NaN\' of undefined at Object.dateUnZero')
        return
      }
      // 获取当前年
      let year = newDate.getFullYear()
      // 获取当前月
      let month = newDate.getMonth() + 1
      // 获取当前日
      let day = newDate.getDate()
      let h = newDate.getHours()
      // 获取当前小时数(0-23)
      let m = newDate.getMinutes()
      // 获取当前分钟数(0-59)
      let s = newDate.getSeconds()
      let res = year + '年' + month + '月'
      if (num > 6) {
        res += day + '日'
      }
      if (num > 8) {
        res += h + '时'
      }
      if (num > 10) {
        res += m + '分'
      }
      if (num > 12) {
        res += s + '秒'
      }
      return res
    }
  }
}
export default dateUtil
